package main.java.com.yc.navy.snacknet.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

public class SendEmailUtil {
//	public SendEmailUtil() {
//		
//	}
	private String sendEmail;
	private String password;
	
	public void setSendEmail(String sendEmail) {
		this.sendEmail = sendEmail;
	}

	public void setPassword(String password) {
		this.password = password;
	}	
	
	public  boolean send(String receive, String code, String nickName) {
		JavaMailSenderImpl sender = new JavaMailSenderImpl();
		sender.setHost("smtp.qq.com");
		sender.setDefaultEncoding("utf-8");
		sender.setUsername(sendEmail);
		sender.setPassword(password);
		
		try {
			MimeMessage mailMessage = sender.createMimeMessage();
			MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage);
			
			messageHelper.setTo(receive);
			messageHelper.setFrom(sendEmail);
			messageHelper.setSubject("零食网注册中心");
			
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss EEE");
			String str="<!doctype html><html><head><meta charset='utf-8'></head><body><p style='font-size: 20px;font-weight:bold;'>尊敬的："+nickName+"，您好！</p>"
					+ "<p style='text-indent:2em; font-size: 20px;'>欢迎注册零食网，您本次的注册码是"
					+ "<span style='font-size: 30px; font-weight: bold;color=red'>" +code+"</span>，3分钟之内有效，请尽快使用！</p>"
					+ "<p style='text-align:right; padding-right: 20px;'>"
					+"<a href='http://www.hyycinfo.com' style='font-size: 18px'></a></p>"
					+ "<span style='font-size: 18px; float: right;margin-right: 20px;'>" +sdf.format(new Date())+ "</span></body></html>";

			
			messageHelper.setText(str, true);
			Properties prop = new Properties();
			prop.put("mail.smtp.auth", true);
			prop.put("mail.smtp.timeout", 25000);
			sender.setJavaMailProperties(prop);
			sender.send(mailMessage);
			return true;
		} catch (MessagingException e) {
			e.printStackTrace();
		}
		
		return false;
		
	}
	
}
